from rest_framework import serializers
from .models import PricingPlan, Subscription, Payment
from users.serializers import UserSerializer


class PricingPlanSerializer(serializers.ModelSerializer):
    name = serializers.CharField(source='membership_type.name', read_only=True)
    prompt_limit = serializers.IntegerField(source='membership_type.prompt_limit', read_only=True)
    duration_months = serializers.SerializerMethodField()
    is_popular = serializers.SerializerMethodField()
    features = serializers.SerializerMethodField()
    
    class Meta:
        model = PricingPlan
        fields = [
            'id', 'name', 'description', 'price', 'duration_days', 'duration_months',
            'prompt_limit', 'features', 'is_active', 'is_popular', 'created_at'
        ]
        read_only_fields = ['created_at']
    
    def get_duration_months(self, obj):
        return round(obj.duration_days / 30)
    
    def get_is_popular(self, obj):
        # 可以根据业务逻辑设置热门标签，这里简单设置中等价位为热门
        return obj.price > 50 and obj.price < 200
    
    def get_features(self, obj):
        # 根据会员类型返回功能列表
        features = [
            f"每月{obj.membership_type.prompt_limit}个提示词",
            "AI智能优化建议",
            "提示词分类管理"
        ]
        if obj.price > 50:
            features.extend([
                "高级模板库",
                "批量导入导出",
                "优先客服支持"
            ])
        if obj.price > 100:
            features.extend([
                "API接口访问",
                "团队协作功能",
                "数据分析报告"
            ])
        return features


class SubscriptionSerializer(serializers.ModelSerializer):
    user_details = UserSerializer(source='user', read_only=True)
    pricing_plan = PricingPlanSerializer(source='membership_type.pricing_plan', read_only=True)
    
    class Meta:
        model = Subscription
        fields = [
            'id', 'user', 'user_details', 'membership_type', 'pricing_plan',
            'start_date', 'end_date', 'status', 'auto_renew',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['created_at', 'updated_at']


class PaymentSerializer(serializers.ModelSerializer):
    user_details = UserSerializer(source='user', read_only=True)
    subscription_details = SubscriptionSerializer(source='subscription', read_only=True)
    
    class Meta:
        model = Payment
        fields = [
            'id', 'user', 'user_details', 'subscription', 'subscription_details',
            'amount', 'payment_method', 'transaction_id', 'status',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['created_at', 'updated_at']


class SubscriptionCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subscription
        fields = ['user', 'membership_type', 'auto_renew']
        
    def create(self, validated_data):
        # 创建订阅
        subscription = Subscription.objects.create(**validated_data)
        return subscription


class PaymentCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Payment
        fields = ['user', 'subscription', 'amount', 'payment_method']
        
    def create(self, validated_data):
        # 创建支付记录
        payment = Payment.objects.create(
            **validated_data,
            status='pending'  # 初始状态为待处理
        )
        return payment